package com.zhaopin.gitlab;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class DownProject {
    static volatile AtomicInteger atomicInteger = new AtomicInteger(0);

    //推荐类
//    static String RPOJECT_NAME_FILE = "dir/gitlab/project.txt";
    //搜索类
//    static String RPOJECT_NAME_FILE = "dir/gitlab/position.txt";

    //运营类
    static String RPOJECT_NAME_FILE = "dir/gitlab/operation.txt";

    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InterruptedException {

        List<String> projectName = FileUtils.readLines(new File(RPOJECT_NAME_FILE), Charset.defaultCharset());
        Runtime runtime = Runtime.getRuntime();

//        runtime.exec("cd /Users/zhaopin/IdeaProjects/project2");

        //新建一个 10 核心数的线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), 40, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        for (String s : projectName) {
            MyThread myThread = new MyThread(runtime,s,atomicInteger.incrementAndGet());
            threadPoolExecutor.submit(myThread);
        }

        threadPoolExecutor.shutdown();
    }

}


@Slf4j
class MyThread implements Callable<Integer> {

    private Runtime runtime;
    private String projectName;
    private int i;

    public MyThread(Runtime runtime, String projectName,int i) {
        this.runtime = runtime;
        this.projectName = projectName;
        this.i = i;
    }


    @Override
    public Integer call() {
        String str = String.format("git clone --recurse-submodules https://gitlab.dev.zhaopin.com/experiment/%s.git", projectName);
        Process exec = null;
        try {
            exec = runtime.exec(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Class<?> unixProcessClazz = null;
        try {
            unixProcessClazz = Class.forName("java.lang.UNIXProcess");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Method waitForMethod = null;
        try {
            waitForMethod = unixProcessClazz.getDeclaredMethod("waitFor");
            log.info(i + ":" + projectName);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        waitForMethod.setAccessible(true);
        synchronized (DownProject.class) {
            try {
                //阻塞执行exec 方法
                waitForMethod.invoke(exec, null);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return i;
    }
}